package com.gwt.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.Maps;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwt.client.event.SlideTransitionEvent;
import com.gwt.client.util.geo.Coordinates;
import com.gwt.client.util.geo.UserLocation;
import com.gwt.client.util.geo.UserLocation.UserLocationCallback;

public class Slide6 extends Composite implements Slide{

	private static final Binder binder = GWT.create(Binder.class);

	interface Binder extends UiBinder<Widget, Slide6> {/***/}

	private Slide.Presenter listener;
	private EventBus eventBus;
	
	@UiField FocusPanel slide;
	@UiField FlowPanel mapContainer;
	@UiField Button getLocation;
	
	private MapWidget map;

	public Slide6(EventBus eventBus) {
		this.eventBus = eventBus;
		initWidget(binder.createAndBindUi(this));
		
		//setup map stuff
		Maps.loadMapsApi("", "2", false, new Runnable() {
			
			@Override
			public void run() {
				setupMap();
			}
		});
	}
	
	@Override
	protected void onLoad() {
		slide.setFocus(true);
		
//		map.setZoomLevel(0);
	}
	
	private void setupMap(){
		map = new MapWidget();
		map.setSize("100%", "100%");
		map.setScrollWheelZoomEnabled(true);
		
		mapContainer.add(map);
	}
	
	@UiHandler("getLocation")
	public void onLocationRequest(ClickEvent e){
		UserLocation.getLocation(new UserLocationCallback() {
			
			@Override
			public void onLocationFound(Coordinates coords) {
				LatLng userLocation = LatLng.newInstance(coords.getLatitude(), coords.getLongitude());
				map.setCenter(userLocation, 12);
				map.addOverlay(new Marker(userLocation));
			}
		});
	}
	
	@UiHandler("slide")
	public void keyPress(KeyDownEvent e){
		eventBus.fireEvent(new SlideTransitionEvent(e));
	}

	public void setPresenter(Slide.Presenter listener){
		this.listener = listener;
	}

}